var PATH_PREFIX = "";

function setPathPrefix(path) {
	PATH_PREFIX = path;
}

var OUT_ORG_MODIFY = function(){
	this.init();
};
/**
 * 初始化方法
 */
OUT_ORG_MODIFY.prototype.init = function(){
	var self = this;
	self.expand();
	
	$("#outOrgModifyForm").validate({
		focusCleanup : true
	});
	
	self.validate();
	
}

/**
 * 拓展方法
 */
OUT_ORG_MODIFY.prototype.expand = function(){
	var self = this;
	
	self.validate = function() {
		$("#companyName").rules("remove");
		$("#companyFullName").rules("remove");
		
		$("#companyName").rules("add", {
			required : true, maxlength4Byte: 400, orgName: true, messages: {required: "请填写机构简称", maxlength4Byte:"机构简称不能大于400个字符"}
		});
		$("#companyFullName").rules("add", {
			required : true, maxlength4Byte: 400, companyFullName: true, messages: {required: "请填写机构全称", maxlength4Byte:"机构全称不能大于400个字符"}
		});
	};
	
	self.submitForm = function() {
		SubmitAndPreventSecond("outOrgModifyForm",function() {
			self.validate();
			
			if(!$("form[name='outOrgModifyForm']").valid()) {
				toastr.warning('', '请完善信息');
				return false;
			}
			
			$('#outOrgModifyForm').attr('action', PATH_PREFIX + "modifyOutOrgExt.do");
			
			return true;
		}, true);
	};
}


jQuery.validator.addMethod("orgName", function(value, element, param) { 
	var flag = false;
    $.ajax({
		  url: PATH_PREFIX + "checkRepeatOrgName.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  orgName : value,
			  orgId: $("#orgId").val()
		  },
		  success: function( data ) {
				if(data.success){
					flag = false;
					return;
				}
				flag = true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				flag = false;
		  }
	});
    if(!flag) {
    	jQuery.validator.messages.orgName = "机构简称已存在";
    }
    return flag;
});
jQuery.validator.addMethod("companyFullName", function(value, element, param) { 
	var flag = false;
    $.ajax({
		  url: PATH_PREFIX + "checkRepeatCompanyFullName.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  companyFullName : value,
			  orgId: $("#orgId").val()
		  },
		  success: function( data ) {
				if(data.success){
					flag = false;
					return;
				}
				flag = true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				flag = false;
		  }
	});
    if(!flag) {
    	jQuery.validator.messages.companyFullName = "机构全称已存在";
    }
    return flag;
});
